package com.atguigu.rabbitmq.springbootmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @description: TODO
 *  配置队列名称 配饰绑定关系
 * @author zhangbanglong
 * @date 2024/1/13 11:44
 * @version 1.0
 */
@Configuration
public class TtlQueueConfig {



    //定义交换机名称和队列名称
    public static  final String X_EXCHANGE="X";
    public static  final String QUEUE_A="QA";
    public static  final String QUEUE_B="QB";

    public static final String Y_EXCHANGE="Y";
    public static final String QUEUE_D="QD";


    //声明交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return  new DirectExchange(X_EXCHANGE);
    }

    //声明Y交换机
    @Bean("yExchange")
    public  DirectExchange yExchange(){
        return new DirectExchange(Y_EXCHANGE);
    }

    //声明队列QA 绑定死信交换机 过期时间10s
    @Bean("queueA")
    public Queue queueA(){
        Map<String,Object> args=new HashMap<>(3);
        args.put("x-dead-letter-exchange",Y_EXCHANGE);
        args.put("x-dead-letter-routing-key","YD");
        args.put("x-message-ttl",10000);
        return QueueBuilder.durable(QUEUE_A).withArguments(args).build();

    }

    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA, @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }


    //声明队列QB 绑定死信交换机 过期时间40s
    @Bean("queueB")
    public Queue queueB(){
        Map<String,Object> args=new HashMap<>(3);
        args.put("x-dead-letter-exchange",Y_EXCHANGE);
        args.put("x-dead-letter-routing-key","YD");
        args.put("x-message-ttl",40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(args).build();

    }


    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,@Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }


    @Bean("queueD")
    public Queue queueD(){
        return new Queue(QUEUE_D);
    }
    @Bean
    public Binding deadLetterBindingQad(@Qualifier("queueD")Queue queueD,@Qualifier("yExchange")DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }



}
